//
//  XNAdministrativeViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/13.
//

#import "XNAdministrativeViewController.h"
#import "XNAdministrationAffExpApproveRequest.h"
#import "XNAdministrativeAffexpRequest.h"
#import "XNAdministrativeBillInfoModel.h"
#import "XNSubTableViewController.h"
#import "XNMoreDataTableViewCell.h"

@interface XNAdministrativeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNAdministrativeBillInfoModel *infoModel;

@end

@implementation XNAdministrativeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNAdministrativeAffexpRequest *infoRequest = [[XNAdministrativeAffexpRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNAdministrativeAffexpRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNAdministrativeAffexpRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNAdministrationAffExpApproveRequest *approveRequest = [[XNAdministrationAffExpApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    if (self.infoModel.mainPageList.count > 0) {
        XNMainPageModel *mainPage = self.infoModel.mainPageList[0];
        itemModel.showOverImage = mainPage.budgetItemObject.remainReimburseMoney < 0;
    }
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeString],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"基本信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.proposerName],
                               [NSString stringWithFormat:@"供应商： %@",self.infoModel.supplierName],
                               [NSString stringWithFormat:@"应付日期： %@",self.infoModel.payDateString],
                               [NSString stringWithFormat:@"交易年月： %@",self.infoModel.expenseDateString],
                               [NSString stringWithFormat:@"币别： %@",self.infoModel.currencyName],
                               [NSString stringWithFormat:@"付款方式： %@",self.infoModel.payWayStr],
                               [NSString stringWithFormat:@"付款用途： %@",self.infoModel.payUse],
                               [NSString stringWithFormat:@"附件合计： %ld",self.infoModel.accessoryCount],
                               [NSString stringWithFormat:@"发票类型： %@",self.infoModel.invoiceTypeName],
                               [NSString stringWithFormat:@"税率： %@",self.infoModel.taxCode],
                               [NSString stringWithFormat:@"报销金额(含税)： %.3f",self.infoModel.amount],
                               [NSString stringWithFormat:@"报销金额(不含税)： %@",self.infoModel.amountWithOutDuty],
                               @"附件信息：",
                               nil];
    itemModel2.showItemArray = showItemArray2;
    itemModel2.fileArray = self.infoModel.fileList;
    itemModel2.fileIsImage = NO;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    

    XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
    reimbursementModel.showType = XNShowType_MoreData;
    reimbursementModel.title = @"报销明细";
    reimbursementModel.showWarning = YES;
    reimbursementModel.warningStr = @"填写的注意事项：\r1、摘要填写：填写简要的人、时间、费用类别，例如：XXX某个月的XXX费用，报销单上会打印出来；\r2、备注填写：详细报销事由及说明，或不适合打印在报销单上的说明内容报销单上不会打印出来。";
    reimbursementModel.headTitleArray = [NSArray arrayWithObjects:
                                         [NSString stringWithFormat:@"预算目录：%@",self.infoModel.budgetDirectoryName],
                                         [NSString stringWithFormat:@"报销年月：%@",self.infoModel.expenseDateString],
                                         [NSString stringWithFormat:@"报销方式(直接报销)：%@",self.infoModel.expenseWay ? @"是":@"否"],
                                         [NSString stringWithFormat:@"报销类别：%@(%@)",self.infoModel.expenseType,self.infoModel.expenseTypeValue],
                                         nil];
    reimbursementModel.showDataArray = [self creatSubTableData];
    [self.showArray addObject:reimbursementModel];
    
    
    
    XNTableShowDataModel *remarkModel = [[XNTableShowDataModel alloc] init];
    remarkModel.showType = XNShowType_Normal;
    remarkModel.title = @"备注";
    if (self.infoModel.remark.length > 0) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:self.infoModel.remark];
        [remarkModel.showDataArray addObject:temp];
    }
    [self.showArray addObject:remarkModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}

- (NSMutableArray *)creatSubTableData
{
    NSMutableArray *array = [NSMutableArray array];
    
    XNTableShowDataModel *showData1 = [[XNTableShowDataModel alloc] init];
    __block CGFloat orginTotal1 = 0;
    __block CGFloat currentTotal1 = 0;
    showData1.title = @"报销主界面";
    [self.infoModel.mainPageList enumerateObjectsUsingBlock:^(XNMainPageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *item = [[XNShowItemModel alloc] init];
        item.showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"预算目录：%@",obj.budgetdirectory],
                              [NSString stringWithFormat:@"部门名称：%@",obj.department],
                              [NSString stringWithFormat:@"部门负责人：%@",obj.principal],
                              [NSString stringWithFormat:@"原始申请金额：%.3f",obj.applyAmount],
                              [NSString stringWithFormat:@"分摊比例(%%)：%.3f",obj.shareRatio],
                              [NSString stringWithFormat:@"报销金额：%.3f",obj.expenseAmount],
                              [NSString stringWithFormat:@"摘要：%@",obj.digest],
                              [NSString stringWithFormat:@"附件张数：%ld",obj.accessoryCount],
                              [NSString stringWithFormat:@"预算说明：%@",obj.budgetItemObject.mainPageShowStr],
                              nil];
        orginTotal1 = orginTotal1 + obj.applyAmount;
        currentTotal1 = currentTotal1 +obj.expenseAmount;
        [showData1.showDataArray addObject:item];
    }];
    showData1.headTitleArray = [NSMutableArray arrayWithObjects:
                      [NSString stringWithFormat:@"合计原始申请金额：%.3f",orginTotal1],
                      [NSString stringWithFormat:@"合计报销金额：%.3f",currentTotal1],
                      nil];
    [array addObject:showData1];
    
    
    XNTableShowDataModel *showData2 = [[XNTableShowDataModel alloc] init];
    __block CGFloat total2 = 0;
    showData2.title = @"报销来源数据";
    [self.infoModel.sourceFormList enumerateObjectsUsingBlock:^(XNSourceFormModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *item = [[XNShowItemModel alloc] init];
        item.showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"来源单号：%@",obj.sourceOrderNo],
                              [NSString stringWithFormat:@"日期：%@",obj.applyDateString],
                              [NSString stringWithFormat:@"申请人：%@",obj.proposer],
                              [NSString stringWithFormat:@"部门：%@",obj.applyDepartment],
                              [NSString stringWithFormat:@"预算项目：%@",obj.budgetdirectory],
                              [NSString stringWithFormat:@"预算项目：%.3f",obj.applyAmount],
                      
                              nil];
        total2 = total2 + obj.applyAmount;
        [showData2.showDataArray addObject:item];
    }];
    showData2.headTitleArray = [NSMutableArray arrayWithObjects:
                      [NSString stringWithFormat:@"合计金额：%.3f",total2],
                      nil];
    [array addObject:showData2];
    
    
    XNTableShowDataModel *showData3 = [[XNTableShowDataModel alloc] init];
    __block CGFloat total3 = 0;
    showData3.title = @"导入EXCEL明细";
    [self.infoModel.importItemList enumerateObjectsUsingBlock:^(XNImportItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *item = [[XNShowItemModel alloc] init];
        item.showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"预算目录：%@",obj.budgetDirectoryName],
                              [NSString stringWithFormat:@"部门：%@",obj.departmentName],
                              [NSString stringWithFormat:@"工号：%@",obj.workNo],
                              [NSString stringWithFormat:@"员工：%@",obj.employeeName],
                              [NSString stringWithFormat:@"金额：%.3f",obj.amount],
                              [NSString stringWithFormat:@"摘要：%@",obj.remark],
                      
                              nil];
        total3 = total3 + obj.amount;
        [showData3.showDataArray addObject:item];
    }];
    showData3.headTitleArray = [NSMutableArray arrayWithObjects:
                      [NSString stringWithFormat:@"合计金额：%.3f",total3],
                      nil];
    [array addObject:showData3];
    
    XNTableShowDataModel *showData4 = [[XNTableShowDataModel alloc] init];
    __block CGFloat total4 = 0;
    showData4.title = @"部门金额汇总";
    [self.infoModel.amountTotalList enumerateObjectsUsingBlock:^(XNAmountTotalModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *item = [[XNShowItemModel alloc] init];
        item.showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"部门名称：%@",obj.departmentName],
                              [NSString stringWithFormat:@"金额：%.3f",obj.totalAmount],
                              nil];
        total4 = total4 + obj.totalAmount;
        [showData4.showDataArray addObject:item];
    }];
    showData4.headTitleArray = [NSMutableArray arrayWithObjects:
                      [NSString stringWithFormat:@"合计金额：%.3f",total4],
                      nil];
    [array addObject:showData4];
    
    return array;
}


@end
